<!--sidebar-menu-->
<div id="sidebar" class="clearfix"><!-- <a href="#" class="visible-phone"><i class="icon icon-home"></i> Dashboard</a> -->
  <ul style="display: block;">
  {% include "common/menu.html" %}
  </ul>
</div>
<!--end sidebar-menu-->

<div id="content">
  
    <div class="container-fluid" style="padding: 0;">

      <div class="widget-box tab-tabs" >
          <div class="widget-title">
              <ul class="nav nav-tabs">
                  {% include "common/tab.html" %}
              </ul>
          </div>
          <div class="widget-content tab-content">
            <div id="tab1" class="tab-pane active">
              <div class="container-fluid">

                  <div class="row-fluid" style="margin: 20px 0;overflow: hidden;">
                    <form id="fiamlyform" class="form-search">
                      <span class="find-search pull-left">
                      <i class="icon-search icon-on-right bigger-110"></i>
                      <input name="name" type="text" placeholder="用户名（uid）或昵称" >
                      <button id="btn_search" type="button" class="btn btn-purple">搜索</button>
                      </span>
                    </form>
                      <form action="/ajax/excelRecIncome" method="post" id="btn_excelForm" class="form-search pull-right">
                          <input class="ExportNickName" type="hidden" value="0" name="nickName">
                          <input class="startDate" type="hidden" value="<?php echo date('Y-m',strtotime('last month')); ?>-11" name="startDate">
                          <input class="endDate" type="hidden" value="<?php echo date('Y-m',strtotime('this month')); ?>-10" name="endDate">
                          <button id="btn_excel" type="submit" class="btn btn-purple">导出表格</button>
                      </form>
                    <a class="date-calendar pull-right" style="margin-right: 10px;height: 30px;line-height: 30px;">
                        <i class=" icon-calendar"></i>
                        <input id="mydate" placeholder="请输入日期" value="<?php echo date('Y-m',strtotime('last month')); ?>-11 - <?php echo date('Y-m',strtotime('this month')); ?>-10">
                    </a>

                      <button type="button" class="btn btn-purple pull-right" style="margin-right: 30px;" onclick="addrec()">新增</button>

                  </div>

                <div class="row-fluid">
                  <table id="tableList" class="table with-check table-purple">
                    <thead>
                      <tr>
                        <th>uid</th>
                        <th>昵称</th>
                        <th>长链接</th>
                        <th>短链接</th>
                        <th>抽成比例</th>
                        <th>抽成期限</th>
                        <th>广告来源</th>
                        <th>广告媒介</th>
                        <th>推广类型</th>
                        <th>累计充值金额</th>
                        <th></th>
                        <th></th>
                        <!--<th></th>-->
                      </tr>
                    </thead>
                    <tbody>

                    </tbody>
                  </table>
                </div>
                  {% include "common/pagination.html" %}
              </div>
            </div>
            
         </div>
        </div>
    
    </div>
            
    
</div>

<div id="addExceptionDialog" style="display:none">
    <form id="roomruleDialogForm">

        <div id="roomconditionsclone" style="display:block;margin-top: 10px;">
            <div class="roomconditions-box">
                <span class="title">广告来源:</span>
                <input id="utm_source"  class="inputlevel" type="text">
            </div>
            <div class="roomconditions-box">
                <span class="title">广告媒介:</span>
                <input id="utm_medium"  class="inputlevel"  type="text">
            </div>
            <div class="roomconditions-box">
                <span class="title">推荐人ID:</span>
                <input id="uid" name="level" class="inputlevel" type="text"><span id="uidW" style="margin-left: 10px;color: red;"></span>
            </div>
            <div id="nickName" class="roomconditions-box" style="display: none;">
                <span class="title">昵称:</span>
                <span id="name"></span>
            </div>
            <div class="roomconditions-box">
                <span  class="title">抽成比例:</span>
                <input id="proportion" class="inputlevel" onkeydown="onlyNum()" type="text"><span style="margin-left: 10px;">%</span>
            </div>
            <div class="roomconditions-box">
                <span class="title">抽成期限:</span>
                <input id="validity" class="inputlevel" onkeydown="onlyNum()" type="text"><span style="margin-left: 10px;">天</span>
            </div>
            <div class="roomconditions-box">
                <span class="title">备注:</span>
                <input id="remark" class="inputlevel" type="text">
            </div>
        </div>

    </form>
</div>

<div id="delrecommendDialog" style="display:none">
    <div style="height: 115px;line-height: 115px;text-align: center;font-size: 16px;">确定删除这个推广链接？</div>
</div>

<div id="addBind" style="display:none">
    <form id="addBindDialogForm">

        <div id="addBindsclone" style="display:block;margin-top: 10px;">

            <div class="roomconditions-box">
                <span class="title">绑定的Uid:</span>
                <input id="addBinduid" name="level" class="inputlevel" type="text" onkeydown="onlyNum()"><span id="addBinduidW" style="margin-left: 10px;color: red;"></span>
            </div>
            <div id="addBindnickName" class="roomconditions-box" style="display: none;">
                <span class="title">昵称:</span>
                <span id="addBindname"></span>
            </div>
            <div id="isBind" class="roomconditions-box">
                <span class="title"></span>
                <span id="isBindP" class="font18"></span>
            </div>

        </div>

    </form>
</div>

<link rel="stylesheet" href="{{static_url('web/css/icon/style-icon.css')}}">
<link rel="stylesheet" href="/web/css/ui.daterangepicker.css">
<script src="/web/js/daterangepicker.jQuery.js"></script>
<script src="{{static_url('web/js/jquery.ui.datepicker-zh-CN.js')}}"></script>

<script type="text/javascript">

$(function(){
    
  getDataList();
    $('#uid').on('blur',checkUid);
    $('#addBinduid').on('blur',checkaddBinduid);
//    $('#proportion').on('blur',checkproportion);
//    $('#uid').on('focus',function(){
//        $('#uidW').html('');
//    });
    $('#mydate').daterangepicker({
                presetRanges:[
                    //{text:'最近一周', dateStart:'yesterday-7days', dateEnd:'yesterday' },
                    //{text:'最近一月', dateStart:'yesterday-1month', dateEnd:'yesterday' },
                    //{text:'最近一年', dateStart:'yesterday-1year', dateEnd:'yesterday' }
                ],
                presets:{
                    dateRange:'自定义时间'
                },
                rangeStartTitle:'起始日期',
                rangeEndTitle:'结束日期',
                nextLinkText:'下月',
                prevLinkText:'上月',
                doneButtonText:'确定',
                cancelButtonText:'取消',
                earliestDate:'',
                latestDate:0,
                constrainDates:true,
                rangeSplitter:'-',
                dateFormat:'yy-mm-dd',
                closeOnSelect:false,
                onOpen:function(){
                    $('.ui-daterangepicker-dateRange').click().parent().hide();
                    $('.ui-daterangepickercontain').css({left:($('#mydate').offset().left-25)+'px'});
                    $('.ui-daterangepickercontain').css({top:($('#mydate').offset().top+30)+'px'});
                    var type=$('#mydate').attr('data');
                    $('.ui-daterangepickercontain .range-end.hasDatepicker').show();
                    if(type == 'hour'){
                        $('.ui-daterangepickercontain .range-end.hasDatepicker').hide();
                    }
                },
                onClose:function(){
                    var b=$('#mydate').val();
                    var timeBegin= b.substring(0,10);
                    var timeEnd= b.substring(12,23);
                    if(!timeEnd||timeEnd==''){
                        timeEnd=timeBegin;
                    }
                    $('.startDate').attr('value',timeBegin);
                    $('.endDate').attr('value',timeEnd);
                }
            }
    );
});

var btn_search=global.doc('btn_search');
utils.addEvent(btn_search,'click',function(){
    getDataList();
});

function getDataList(index){
    var form=global.doc('fiamlyform');
    $('.ExportNickName').attr('value',form.name.value);
    var data={};
    data.search=form.name.value;
    data.sort=1;
    data.page=index||1;
    data.pageSize=global.pageSize;
    $.ajax({
        type:"POST",
        data:data,
        url:'/ajax/getUserRecList',
        dataType:'json',
        success:function(res){
            var data=res.data;
            var tableList=document.getElementById("tableList");
            var tbody= tableList.getElementsByTagName('tbody')[0];
            dataTable.clearData(tbody);

            var count=document.getElementById('count');
            count.innerHTML=data.count;

            for(var i=0;i<data.data.length;i++){
                addRow(tbody,data.data[i]);
            }
            if(!index||index==0){
                utilsPagination("#pagination",data.count,global.pageSize,function(page_index,jq){
                    page_index+=1;
                    getDataList(page_index);
                });
            }
        }

    });
}


function addRow(table,data){

  var row=table.insertRow();
  var cell=row.insertCell(0);
  var cell1=row.insertCell(1);
  var cell2=row.insertCell(2);
  var cell3=row.insertCell(3);
  var cell4=row.insertCell(4);
  var cell5=row.insertCell(5);
  var cell6=row.insertCell(6);
  var cell7=row.insertCell(7);
  var cell8=row.insertCell(8);
  var cell9=row.insertCell(9);
  var cell10=row.insertCell(10);
  var cell11=row.insertCell(11);
//  var cell12=row.insertCell(12);

  var div=document.createElement("div"),
    img=document.createElement("img"),
    span=document.createElement("span");

    div.className="anchor-info";
    img.src=global.contract+data.avatar;
    span.innerHTML=data.uid;

    div.appendChild(img);
    div.appendChild(span);

    cell.appendChild(div);

    cell1.innerHTML=data.nickName;   

    cell2.innerHTML=data.longUrl;
    cell3.innerHTML=data.tinyUrl;
    cell4.innerHTML=data.proportion+'%';
    cell5.innerHTML=data.validity;
    cell6.innerHTML=data.utmSource;
    cell7.innerHTML=data.utmMedium;
    cell8.innerHTML=(data.type==1)?'新用户推广':'广告推广链接';
    cell9.innerHTML=data.allCash;

    
    var a=document.createElement("a");
    a.href='/pull/details?uid='+data.uid+'&id='+data.id;
    a.innerHTML="查看";
    a.className="a-edit font font18 cr-pe cursor-pointer";
    cell10.appendChild(a);

//    cell11.innerHTML='<a class="a-edit font font18 cr-pe cursor-pointer" onclick="delrecommendDialog('+data.id+')">删除</a>';
    cell11.innerHTML='<a class="a-edit font font18 cr-pe cursor-pointer" onclick="addrecBind('+data.uid+')">绑定</a>';
    return row;
}

//  新用户推荐新增
function addrec(){
  $("#addExceptionDialog").dialog({
        autoOpen:true,
        width:450,
        height:430,
        title:'新增',
        resizable:false,
        modal:true,
        open:function(){
        },
        buttons: {
            "生成": function () {
                var data={};
                data.utm_source = $('#utm_source').val();
                data.utm_medium = $('#utm_medium').val();
                data.uid = $('#uid').val();
                data.proportion = $('#proportion').val();
                data.validity = $('#validity').val();
                data.remark = $('#remark').val();

                if($('#utm_source').val()==''||$('#utm_medium').val()=='') {
                    alert('请正确填写信息');
                    return false;
                }
                if($('#uidW').attr('data')==1){
                    alert('请正确uid');
                    return false;
                }
                if($('#uid').val()!=''){
                    if($('#proportion').val()==''){
                        alert('请正确填写抽成比例');
                        return false;
                    }
                    if($('#validity').val()==''){
                        alert('请正确填写抽成期限');
                        return false;
                    }
                }

                if($('#proportion').val()!=''){
                    if($('#proportion').val()>100||$('#proportion').val()<1){
                        alert('抽成比例限1-100的数字');
                        return false;
                    }
                }

                $.ajax({
                    type:"POST",
                    data:data,
                    url:'/ajax/addRecUrl',
                    dataType:'json',
                    success:function(res){
                        if(res.code == 0) {
                            location.reload();
                        }else{
                            alert(res.info);
                        }
                    }
                });

            },
            "取消": function () {

                $(this).dialog("close");
            }
        }
    });
}

function checkUid(){
    var data={};
    data.uid = $('#uid').val();
    if($('#uid').val()==''){
        return false;
    }
    $.ajax({
        type:"POST",
        data:data,
        url:'/ajax/getNicknameByUid',
        dataType:'json',
        success:function(res){
            if(res.code != 0) {
                $('#uidW').html('用户不存在');
                $('#uidW').attr('data',1);
            }else{
                $('#uidW').html('');
                $('#name').html(res.data.nickName);
                $('#nickName').show();
                $('#uidW').attr('data',0);
            }
        }
    });

}

//  新用户推荐删除
function delrecommendDialog(id){

    $('#delrecommendDialog').dialog({
        autoOpen:true,
        width:440,
        height:250,
        title:'删除',
        resizable:false,
        modal:true,
        open:function(){
        },
        buttons: {
            "确定": function () {
                var data={};
                data.id=id;
                $.ajax({
                    type:"POST",
                    data:data,
                    url:'/ajax/delRec',
                    dataType:'json',
                    success:function(res){
                        if(res.code!=0){
                            alert(res.info);
                        }
                        location.reload();
                    }

                });

            }
        }
    });
}


function checkproportion(){
    var a=$('#proportion').val();
    if(a>100||a<1){
        $('#proportion').attr('data',1);
        alert('限1-100的数字');
    }
    else{
        $('#proportion').attr('data',0);
    }
}

//  绑定
function addrecBind(uid){
    $("#addBind").dialog({
        autoOpen:true,
        width:450,
        height:280,
        title:'绑定',
        resizable:false,
        modal:true,
        open:function(){
        },
        buttons: {
            "绑定": function () {
                var data={};
                data.recUid = uid;
                data.beRecUid = $('#addBinduid').val();
                checkaddBinduid();
                if($('#addBinduid').val()=='' || $('#addBinduidW').attr('data')==1){

                    alert('该用户不可绑定，请重新输入用户uid');
                    return false;
                }

//                if($('#utm_source').val()==''||$('#utm_medium').val()=='') {
//                    alert('请正确填写信息');
//                    return false;
//                }
//                if($('#uidW').attr('data')==1){
//                    alert('请正确uid');
//                    return false;
//                }
//
                $.ajax({
                    type:"POST",
                    data:data,
                    url:'/ajax/bindRec',
                    dataType:'json',
                    success:function(res){
                        if(res.code == 0) {
                            location.reload();
                        }else{
                            alert(res.info);
                        }
                    }
                });

            },
            "取消": function () {

                $(this).dialog("close");
            }
        }
    });
}

function checkaddBinduid(){
    var data={};
    data.uid = $('#addBinduid').val();
    if($('#addBinduid').val()==''){
        return false;
    }
    $.ajax({
        type:"POST",
        data:data,
        url:'/ajax/getBeRecInfo',
        dataType:'json',
        success:function(res){
            if(res.code != 0) {
                $('#addBinduidW').html('用户不存在');
                $('#addBinduidW').attr('data',1);
                return false;
            }else{
                $('#addBinduidW').html('');
                $('#addBindname').html(res.data.nickName);
                $('#addBindnickName').show();
                $('#addBinduidW').attr('data',0);

                if(res.data.hasbind==1){
                    $('#isBindP').html('该用户不可绑定');
                    $('#isBindP').css('color','red');
                    $('#addBinduidW').attr('data',1);
                    return false;
                }
                else{
                    $('#isBindP').html('该用户可以绑定');
                    $('#isBindP').css('color','green');
                    $('#addBinduidW').attr('data',0);
                    return false;
                }
            }
        }
    });

}

</script>